package com.atguigu.scw.manager.service.impl;

import com.atguigu.scw.manager.bean.TPermission;
import com.atguigu.scw.manager.bean.TRole;
import com.atguigu.scw.manager.bean.TRoleExample;
import com.atguigu.scw.manager.bean.TRolePermission;
import com.atguigu.scw.manager.bean.TRolePermissionExample;
import com.atguigu.scw.manager.dao.TRoleMapper;
import com.atguigu.scw.manager.dao.TRolePermissionMapper;
import com.atguigu.scw.manager.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    TRoleMapper roleMapper;
    @Autowired
    TRolePermissionMapper rolePermissionMapper;

    @Override
    public List<TRole> getAllRole() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public List<TRole> getUserRole(Integer userId) {
        return roleMapper.selectByUserId(userId);
    }

    @Override
    public List<TPermission> getRolePermission(Integer roleId) {
        return roleMapper.getRolePermission(roleId);
    }

    @Override
    public boolean updateRolePermission(Integer roleId, String permissionIds) {
        // 先删除
        TRolePermissionExample tRolePermissionExample = new TRolePermissionExample();
        TRolePermissionExample.Criteria criteria = tRolePermissionExample.createCriteria();
        criteria.andRoleidEqualTo(roleId);
        rolePermissionMapper.deleteByExample(tRolePermissionExample);
        // 再添加
        String[] split = permissionIds.split(",");
        if (split.length >= 1) {
            for (String s : split) {
                TRolePermission tRolePermission = new TRolePermission();
                tRolePermission.setRoleid(roleId);
                tRolePermission.setPermissionid(Integer.parseInt(s));
                rolePermissionMapper.insertSelective(tRolePermission);
            }
            return true;
        }
        return false;
    }

    @Override
    public List<TRole> listRolePage(HashMap<String, Object> paramMap) {
        String condition = (String) paramMap.get("condition");
        if (StringUtils.hasText(condition)) {
            TRoleExample example = new TRoleExample();
            TRoleExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%" + condition + "%");
            return roleMapper.selectByExample(example);
        }
        return roleMapper.selectByExample(null);
    }

    @Override
    public void saveRole(TRole role) {
        long l = roleMapper.countByExample(null);
        if (role.getId() == null) {
            role.setId((int) l + 1);
        }
        roleMapper.insertSelective(role);
    }

    @Override
    public TRole getRoleById(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public void updateRole(TRole role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void deleteRole(Integer roleId) {
        roleMapper.deleteByPrimaryKey(roleId);
    }
}
